//
//  AppDelegate.m
//  MMLOOK
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "Referrer.h"
#import "WeddingDream.h"
#import "MY.h"
#import "MyMusicViewController.h"

#import "UMSocial.h"
#import "ZWIntroductionViewController.h"

BOOL isOut = NO;
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic, retain)ZWIntroductionViewController *introduceView;
@property (nonatomic, retain)UIPageControl *page;
@property (nonatomic, retain)NSArray *arr;
@end

@implementation AppDelegate

- (void)dealloc
{
    [_page release];
    [_arr release];
    [_introduceView release];
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    //homepage
    ViewController *view = [[[ViewController alloc] init] autorelease];
    
    UINavigationController *first = [[[UINavigationController alloc] initWithRootViewController:view] autorelease];
    
    UIImage *homepageone = [UIImage imageNamed:@"homepageone.png"];
    
    UIImage *homepageonenew = [homepageone imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *homepagetwo = [UIImage imageNamed:@"homepagetwo.png"];
    
    UIImage *homepagetwonew = [homepagetwo imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    view.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:homepageonenew selectedImage:homepagetwonew];
    
    
    //referrer
    Referrer *referView = [[[Referrer alloc] init] autorelease];
    
    UINavigationController *referControl = [[[UINavigationController alloc] initWithRootViewController:referView] autorelease];
    
    UIImage *referimage = [UIImage imageNamed:@"referone.png"];
    
    UIImage *referimagenew = [referimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *refertwo = [UIImage imageNamed:@"refertwo.png"];
    
    UIImage *refertwonew = [refertwo imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    referView.tabBarItem = [[[UITabBarItem alloc] initWithTitle:nil image:referimagenew selectedImage:refertwonew] autorelease];
    
    //WeddingDream
    
    WeddingDream *wedView = [[[WeddingDream alloc] init] autorelease];
    
    UINavigationController *wedControl = [[[UINavigationController alloc] initWithRootViewController:wedView] autorelease];
    
    UIImage *wedimage = [UIImage imageNamed:@"weddingone.png"];
    
    UIImage *wedimagenew = [wedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *wedimagetwo = [UIImage imageNamed:@"weddingtwo.png"];
    
    UIImage *wedimagetwonew = [wedimagetwo imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    wedView.tabBarItem = [[[UITabBarItem alloc] initWithTitle:nil image:wedimagenew selectedImage:wedimagetwonew] autorelease];
    
    
    //MY
    
    MY *myView = [[[MY alloc] init] autorelease];
    
    UINavigationController *myController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease];
    
    UIImage *myimage = [UIImage imageNamed:@"myone.png"];
    
    UIImage *myimagenew = [myimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *myimagetwo = [UIImage imageNamed:@"mytwo.png"];
    
    UIImage *myimagetwonew = [myimagetwo imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    myView.tabBarItem = [[[UITabBarItem alloc] initWithTitle:nil image:myimagenew selectedImage:myimagetwonew] autorelease];
    
    
    //myMusic
    
    MyMusicViewController *music = [[MyMusicViewController alloc] init];
    UINavigationController *musicNC = [[[UINavigationController alloc] initWithRootViewController:music] autorelease];
    
    UIImage *musicImage = [UIImage imageNamed:@"yinyue.png"];
    
    UIImage *musicNew = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   
    
    musicNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:nil image:musicNew selectedImage:nil] autorelease];

    
    
    
    NSMutableArray *array = [NSMutableArray array];
    
    [array addObject:first];
    
    [array addObject:referControl];
    
    [array addObject:wedControl];
    
    [array addObject:musicNC];
    
    [array addObject:myController];
    
    
    
    UITabBarController *tab = [[[UITabBarController alloc] init] autorelease];
//    UIImage *tabimage = [UIImage imageNamed:@"tab.png"];
//    UIImage *atabimage = [tabimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    tab.tabBar.backgroundImage = atabimage;
    [tab setViewControllers:array];
    tab.tabBar.backgroundColor = [UIColor colorWithRed:253 / 255.0 green:180/255.0 blue:83/255.0 alpha:1];
    [self.window setRootViewController:tab];
    
    
    //设置友盟社会化组件appkey
    [UMSocialData setAppKey:@"55eae07167e58e4f0b00473f"];
    //如需要支持不同的屏幕方向，需要这样设置，否则在iPhone只支持竖屏方向
    [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskAll];
    
//    NSArray *coverimageName = @[@"0.png", @"0.png", @"0.png"];
//    NSArray *backgroundImageName = @[@"first.png", @"second.png", @"third.png"];
//    self.introduceView = [[[ZWIntroductionViewController alloc] initWithCoverImageNames:coverimageName backgroundImageNames:backgroundImageName] autorelease];
//    
//    [self.window addSubview: self.introduceView.view];
//    __weak AppDelegate *weakSelf = self;
//    self.introduceView.didSelectedEnter = ^(){
//        [weakSelf.introduceView.view removeFromSuperview];
//        weakSelf.introduceView = nil;
//    };

    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
     //NSError *error = nil;
    NSString *path = [arr firstObject];
    path = [NSString stringWithFormat:@"%@/aa.txt",path];
    NSFileManager *manager=[NSFileManager defaultManager];
    
    BOOL isHasFile=[manager fileExistsAtPath:path];
    
    if (isHasFile) {
        [self gotoMain]; //为真表示已有文件 曾经进入过主页
    }else{
        [self makeLaunchView];//为假表示没有文件，没有进入过主页
        NSString *str= @"进入过主页";
        [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    
    [self addPage];
    return YES;
}

- (void)makeLaunchView
{
    self.arr=[NSArray arrayWithObjects:@"first.png",@"second.png", @"third.png",nil];//数组内存放的是我要显示的假引导页面图片
    //通过scrollView 将这些图片添加在上面，从而达到滚动这些图片
    UIScrollView *scr = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    scr.contentSize = CGSizeMake(self.window.frame.size.width * _arr.count + 30, self.window.frame.size.height);
    scr.pagingEnabled = YES;
    scr.tag = 7000;
    scr.delegate = self;
    [self.window addSubview:scr];
    for (int i=0; i<_arr.count; i++) {
        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(i * self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height)];
        img.image=[UIImage imageNamed:_arr[i]];
        [scr addSubview:img];
        [img release];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //这里是在滚动的时候判断 我滚动到哪张图片了，如果滚动到了最后一张图片，那么
    //如果在往下面滑动的话就该进入到主界面了，我这里利用的是偏移量来判断的，当
    //一共五张图片，所以当图片全部滑完后 又像后多滑了30 的时候就做下一个动作
    if (scrollView.contentOffset.x > (_arr.count - 1) * self.window.frame.size.width + 30) {
        
        isOut=YES;//这是我声明的一个全局变量Bool 类型的，初始值为no，当达到我需求的条件时将值改为yes
        
    }
}

/* 创建page */
- (void)addPage
{
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.window.frame.size.width / 2 - 50, self.window.frame.size.height - 80, 100, 40)];
    [self.window addSubview:self.page];
    self.page.currentPage = 0;
    self.page.numberOfPages = _arr.count;
    self.page.backgroundColor = [UIColor clearColor];
    [_page release];
    
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    self.page.currentPage = index;
    
    //判断isout为真 就要进入主界面了
    if (isOut) {
        //这里添加了一个动画，（可根据个人喜好）
        [UIView animateWithDuration:0.7 animations:^{
            scrollView.alpha=0;//让scrollview 渐变消失
            
        }completion:^(BOOL finished) {
            [scrollView  removeFromSuperview];//将scrollView移除
            [self gotoMain];//进入主界面
            
        } ];
    }
    
}

- (void)gotoMain
{
    self.page.hidden = YES;
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
